/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.image.ImageUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel.test
 * @date 08/02/2014 - 11:14:54
 */
public class ImagePanel extends JPanel implements JXComponent {
	
	private static final double DEFAULT_SCALE = 1.0;
	
	private static final long serialVersionUID = -6190628010514188694L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	private BufferedImage bufferedImage;
	
	private double scale = DEFAULT_SCALE;
	
	private byte[] dataByte;
	
	/**
	 * 
	 */
	public ImagePanel() {
	
		this(ImageUtil.newImage(1, 1));
	}
	
	/**
	 * @param bufferedImage
	 */
	public ImagePanel(final BufferedImage bufferedImage) {
	
		this.bufferedImage = ImageUtil.copy(bufferedImage);
		
		//
		setPreferredSize(getPreferredSize());
		setBackground(Color.WHITE);
	}
	
	/**
	 * @param imageIcon
	 */
	public ImagePanel(final ImageIcon imageIcon) {
	
		this(ImageUtil.copy(imageIcon));
	}
	
	/**
	 * @param imageIcon
	 * @return scale
	 */
	private double generateScale(ImageIcon imageIcon) {
	
		final double panelArea = getWidth() * getHeight();
		final double imageArea = imageIcon.getIconWidth() * imageIcon.getIconHeight();
		
		//
		if(panelArea < imageArea) {
			
			return imageIcon.getIconWidth() > imageIcon.getIconHeight() ? getWidth() / (double)imageIcon.getIconWidth() : getHeight() / (double)imageIcon.getIconHeight();
			
		}else {
			
			return DEFAULT_SCALE;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(final Graphics graphics) {
	
		super.paintComponent(graphics);
		
		final Graphics2D graphics2d = (Graphics2D)graphics;
		
		graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		
		final double x = bufferedImage == null ? 0 : (getWidth() - getScale() * bufferedImage.getWidth()) / 2;
		final double y = bufferedImage == null ? 0 : (getHeight() - getScale() * bufferedImage.getHeight()) / 2;
		
		final AffineTransform affineTransform = AffineTransform.getTranslateInstance(x, y);
		
		affineTransform.scale(getScale(), getScale());
		
		graphics2d.drawRenderedImage(bufferedImage, affineTransform);
	}
	
	/**
	 * @param value
	 */
	public final void downScale(final double value) {
	
		final double newScale = getScale() - value;
		
		setScale(newScale > 0 ? newScale : getScale());
	}
	
	/**
	 * @return imageIcon in byte array or null
	 */
	public final byte[] getByteArray() {
	
		return dataByte;
	}
	
	/**
	 * @return imageIcon
	 */
	public final ImageIcon getImage() {
	
		return bufferedImage == null ? null : new ImageIcon(bufferedImage);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#getIndex()
	 */
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/**
	 * For the scroll pane.
	 */
	@Override
	public final Dimension getPreferredSize() {
	
		final int width = bufferedImage == null ? 0 : (int)(getScale() * bufferedImage.getWidth());
		final int height = bufferedImage == null ? 0 : (int)(getScale() * bufferedImage.getHeight());
		
		return new Dimension(width, height);
	}
	
	/**
	 * @return scale
	 */
	public final double getScale() {
	
		return scale;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isAtributte()
	 */
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequired()
	 */
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setAtributte(boolean)
	 */
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	/**
	 * @param dataByte
	 */
	public final void setImage(final byte[] dataByte) {
	
		setImage(dataByte, false);
	}
	
	/**
	 * @param dataByte
	 * @param adjustingScale
	 */
	public final void setImage(final byte[] dataByte, final boolean adjustingScale) {
	
		this.dataByte = dataByte;
		
		ImageIcon imageIcon = ImageUtil.toImageIcon(dataByte);
		
		bufferedImage = ImageUtil.copy(imageIcon);
		
		setBackground(Color.WHITE);
		
		//
		if(adjustingScale) {
			
			setScale(generateScale(imageIcon));
			
		}else {
			
			setScale(DEFAULT_SCALE);
		}
	}
	
	/**
	 * @param imageIcon
	 */
	public final void setImage(final ImageIcon imageIcon) {
	
		setImage(imageIcon, DEFAULT_SCALE);
	}
	
	/**
	 * @param imageIcon
	 * @param adjustingScale
	 */
	public final void setImage(final ImageIcon imageIcon, final boolean adjustingScale) {
	
		//
		if(adjustingScale) {
			
			setImage(imageIcon, generateScale(imageIcon));
			
		}else {
			
			setImage(imageIcon, DEFAULT_SCALE);
		}
	}
	
	/**
	 * @param imageIcon
	 * @param newScale
	 */
	public final void setImage(final ImageIcon imageIcon, final double newScale) {
	
		this.dataByte = ImageUtil.toByteArray(imageIcon);
		
		bufferedImage = ImageUtil.copy(imageIcon);
		
		setBackground(Color.WHITE);
		
		setScale(newScale);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setIndex(int)
	 */
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequired(boolean)
	 */
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Panel.border"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
	
	/**
	 * @param newScale
	 */
	public final void setScale(final double newScale) {
	
		scale = newScale;
		
		revalidate(); // update the scroll pane
		repaint();
	}
	
	/**
	 * @param value
	 */
	public final void upScale(final double value) {
	
		setScale(getScale() + value);
	}
}
